
package com.enhe.gck.service.impl;

import com.enhe.gck.communal.PublicConstant;
import com.enhe.gck.controller.dto.WeightingDTO;
import com.enhe.gck.item.WeighHWANE16720Item;
import com.enhe.gck.pojo.WeighOther;
import com.enhe.gck.utils.DateUtil;
import com.kuangkie.carbon.panel.CarbonPanel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

import static com.enhe.gck.CarbonBaseConstant.过磅信息_gbxx;

/**
 * 过磅记录service
 */
@Service
public class WeighService {

    /**
     * 获取订单皮重过磅记录
     */
    public Optional<WeighOther> getTareWeighByOrderNo(String orderNo, String orderType) {
        // 是采购
        boolean isSaleOrder = PublicConstant.OrderTypeSale.equals(orderType);
        return getWeighByOrderNo(orderNo, isSaleOrder ? 1 : 2);
    }

    /**
     * 获取订单毛重过磅记录
     */
    public Optional<WeighOther> getGrossWeighByOrderNo(String orderNo, String orderType) {
        // 是采购
        boolean isSaleOrder = PublicConstant.OrderTypeSale.equals(orderType);
        return getWeighByOrderNo(orderNo, isSaleOrder ? 2 : 1);
    }

    private Optional<WeighOther> getWeighByOrderNo(String orderNo, int weighCount) {
        WeighOther weighOtherRecord = CarbonPanel.getRecordQueryFactory(过磅信息_gbxx).getProConJunctionFactory()
                .addEqual(WeighHWANE16720Item.基本属性组_是否正常, true)
                .addEqual(WeighHWANE16720Item.基本属性组_过磅次数, weighCount)
                .addEqual(WeighHWANE16720Item.基本属性组_订单号, orderNo)
                .goBack().queryABean(WeighOther.class);
        if (null == weighOtherRecord) {
            return Optional.empty();
        }
        return Optional.of(weighOtherRecord);
    }

    /**
     * 保存过磅失败过磅记录(不受事务控制)
     *
     * @param dto               过磅参数
     * @param orderNo           订单号
     * @param carNo             车牌号
     * @param exceptionTypeLong 过磅异常类型
     * @param exceptionNum      过磅异常数量
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public synchronized void saveFailWeighRecord(WeightingDTO dto, String orderNo, String remark, String carNo, Long exceptionTypeLong, Integer exceptionNum) {
        WeighOther weighOtherRecord = new WeighOther();
        weighOtherRecord.setWeightTare(dto.getTare());
        weighOtherRecord.setWeightGross(dto.getGross());
        weighOtherRecord.setWeight(dto.getNet());
        weighOtherRecord.setFrequency(dto.getFrequency());
        weighOtherRecord.setIsOk(false);
        weighOtherRecord.setOrderNumber(orderNo);
        weighOtherRecord.setPassTime(LocalDateTime.now());
        // 设置失败原因
        weighOtherRecord.setRemark(remark);
        // 设置相关车牌号
        weighOtherRecord.setCarNumber(carNo);

        weighOtherRecord.setLaneId(dto.getLaneId());
        weighOtherRecord.setLaneNo(dto.getLaneNo());
        weighOtherRecord.setLaneName(dto.getLaneName());
        weighOtherRecord.setExceptionType(Objects.nonNull(exceptionTypeLong) ? String.valueOf(exceptionTypeLong) : null);
        weighOtherRecord.setExceptionNumber(exceptionNum);
        weighOtherRecord.setLoadStartTime(Objects.nonNull(dto.getLoadStartTimeStr()) ? DateUtil.strToLocalDateTime(dto.getLoadStartTimeStr()) : null);
        weighOtherRecord.setLoadEndTime(Objects.nonNull(dto.getLoadEndTimeStr()) ? DateUtil.strToLocalDateTime(dto.getLoadEndTimeStr()) : null);
        // 保存过磅失败信息
        CarbonPanel.getRecordIntegration(过磅信息_gbxx).integrate(weighOtherRecord);
    }
}
